﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using QLVatTu.Class;
using QLVatTu.DanhMuc;
using QLVatTu.HeThong;
using QLVatTu.NghiepVu;
using QLVatTu.BaoCao;

namespace QLVatTu
{
    public partial class frmMain : Office2007RibbonForm
    {
        public static frmMain run_frmMain;
        KetNoiDB cn = new KetNoiDB();

        public frmMain()
        {
            InitializeComponent();
            run_frmMain = this;
        }

        public void OpenTabForm(Form vForm)
        {
            bool exist = false;
            foreach (TabItem tbi in tabMain.Tabs)
            {
                if (tbi.Text == vForm.Text)
                {
                    exist = true;
                    tabMain.SelectedTab = tbi;
                    break;
                }
            }
            if (!exist)
            {
                TabItem tbi = tabMain.CreateTab(vForm.Text);
                vForm.FormBorderStyle = FormBorderStyle.None;
                vForm.TopLevel = false;
                vForm.Dock = DockStyle.Fill;
                vForm.Parent = tbi.AttachedControl;
                vForm.Show();
                tabMain.SelectedTab = tbi;
            }
        }

        public void HienThiChucNangTheoQuyen()
        {
            DataTable dt = new DataTable();
            string _MaQuyenHan = HangSo.MaQuyenHan;

            /** Quản lý hệ thống **/
            // Đổi mật khẩu
            btnDoiMatKhau.Enabled = false;
            dt = cn.getTable("SELECT * FROM tbl_PhanQuyen WHERE MaQuyenHan='" + _MaQuyenHan + "' AND MaCapPhep='CP001'");
            if (dt.Rows.Count > 0) btnDoiMatKhau.Enabled = true;
            // Quản lý người dùng
            btnQuanLyNguoiDung.Enabled = false;
            dt = cn.getTable("SELECT * FROM tbl_PhanQuyen WHERE MaQuyenHan='" + _MaQuyenHan + "' AND MaCapPhep='CP002'");
            if (dt.Rows.Count > 0) btnQuanLyNguoiDung.Enabled = true;
            // Quản lý quyền
            btnQuyenHan.Enabled = false;
            dt = cn.getTable("SELECT * FROM tbl_PhanQuyen WHERE MaQuyenHan='" + _MaQuyenHan + "' AND MaCapPhep='CP003'");
            if (dt.Rows.Count > 0) btnQuyenHan.Enabled = true;
            // Quản lý phân quyền
            btnPhanQuyen.Enabled = false;
            dt = cn.getTable("SELECT * FROM tbl_PhanQuyen WHERE MaQuyenHan='" + _MaQuyenHan + "' AND MaCapPhep='CP004'");
            if (dt.Rows.Count > 0) btnPhanQuyen.Enabled = true;
            // Sao lưu dữ liệu
            btnSaoLuu.Enabled = false;
            dt = cn.getTable("SELECT * FROM tbl_PhanQuyen WHERE MaQuyenHan='" + _MaQuyenHan + "' AND MaCapPhep='CP005'");
            if (dt.Rows.Count > 0) btnSaoLuu.Enabled = true;
            // Phục hồi dữ liệu
            btnPhucHoi.Enabled = false;
            dt = cn.getTable("SELECT * FROM tbl_PhanQuyen WHERE MaQuyenHan='" + _MaQuyenHan + "' AND MaCapPhep='CP006'");
            if (dt.Rows.Count > 0) btnPhucHoi.Enabled = true;
            // Cấu hình hệ thống
            btnCauHinh.Enabled = false;
            dt = cn.getTable("SELECT * FROM tbl_PhanQuyen WHERE MaQuyenHan='" + _MaQuyenHan + "' AND MaCapPhep='CP007'");
            if (dt.Rows.Count > 0) btnCauHinh.Enabled = true;
            // Lịch sử hệ thống
            btnLichSuHeThong.Enabled = false;
            dt = cn.getTable("SELECT * FROM tbl_PhanQuyen WHERE MaQuyenHan='" + _MaQuyenHan + "' AND MaCapPhep='CP032'");
            if (dt.Rows.Count > 0) btnLichSuHeThong.Enabled = true;

            /** Quản lý danh mục **/
            // Quản lý nhóm vật tư
            btnNhomVatTu.Enabled = false;
            dt = cn.getTable("SELECT * FROM tbl_PhanQuyen WHERE MaQuyenHan='" + _MaQuyenHan + "' AND MaCapPhep='CP008'");
            if (dt.Rows.Count > 0) btnNhomVatTu.Enabled = true;
            // Quản lý vật tư
            btnVatTu.Enabled = false;
            dt = cn.getTable("SELECT * FROM tbl_PhanQuyen WHERE MaQuyenHan='" + _MaQuyenHan + "' AND MaCapPhep='CP009'");
            if (dt.Rows.Count > 0) btnVatTu.Enabled = true;
            // Quản lý kho
            btnKho.Enabled = false;
            dt = cn.getTable("SELECT * FROM tbl_PhanQuyen WHERE MaQuyenHan='" + _MaQuyenHan + "' AND MaCapPhep='CP010'");
            if (dt.Rows.Count > 0) btnKho.Enabled = true;
            // Quản lý đơn vị tính
            btnDonViTinh.Enabled = false;
            dt = cn.getTable("SELECT * FROM tbl_PhanQuyen WHERE MaQuyenHan='" + _MaQuyenHan + "' AND MaCapPhep='CP011'");
            if (dt.Rows.Count > 0) btnDonViTinh.Enabled = true;
            // Quản lý nhân viên
            btnNhanVien.Enabled = false;
            dt = cn.getTable("SELECT * FROM tbl_PhanQuyen WHERE MaQuyenHan='" + _MaQuyenHan + "' AND MaCapPhep='CP012'");
            if (dt.Rows.Count > 0) btnNhanVien.Enabled = true;
            // Quản lý phòng ban
            btnPhongBan.Enabled = false;
            dt = cn.getTable("SELECT * FROM tbl_PhanQuyen WHERE MaQuyenHan='" + _MaQuyenHan + "' AND MaCapPhep='CP013'");
            if (dt.Rows.Count > 0) btnPhongBan.Enabled = true;
            // Quản lý chức vụ
            btnChucVu.Enabled = false;
            dt = cn.getTable("SELECT * FROM tbl_PhanQuyen WHERE MaQuyenHan='" + _MaQuyenHan + "' AND MaCapPhep='CP014'");
            if (dt.Rows.Count > 0) btnChucVu.Enabled = true;
            // Quản lý đối tác
            btnDoiTac.Enabled = false;
            dt = cn.getTable("SELECT * FROM tbl_PhanQuyen WHERE MaQuyenHan='" + _MaQuyenHan + "' AND MaCapPhep='CP015'");
            if (dt.Rows.Count > 0) btnDoiTac.Enabled = true;
            // Quản lý vật tư - nhà cung cấp
            btnNhaCungCap.Enabled = false;
            dt = cn.getTable("SELECT * FROM tbl_PhanQuyen WHERE MaQuyenHan='" + _MaQuyenHan + "' AND MaCapPhep='CP016'");
            if (dt.Rows.Count > 0) btnNhaCungCap.Enabled = true;
            // Quản lý công trình - dự án
            btnCongTrinhDuAn.Enabled = false;
            dt = cn.getTable("SELECT * FROM tbl_PhanQuyen WHERE MaQuyenHan='" + _MaQuyenHan + "' AND MaCapPhep='CP017'");
            if (dt.Rows.Count > 0) btnCongTrinhDuAn.Enabled = true;

            /** Quản lý nghiệp vụ **/
            // Quản lý vật tư dự trù
            btnQuanLyVatTuDuTru.Enabled = false;
            dt = cn.getTable("SELECT * FROM tbl_PhanQuyen WHERE MaQuyenHan='" + _MaQuyenHan + "' AND MaCapPhep='CP018'");
            if (dt.Rows.Count > 0) btnQuanLyVatTuDuTru.Enabled = true;
            // Quản lý yêu cầu nhập vật tư
            btnQuanLyYeuCauNhap.Enabled = false;
            dt = cn.getTable("SELECT * FROM tbl_PhanQuyen WHERE MaQuyenHan='" + _MaQuyenHan + "' AND MaCapPhep='CP019'");
            if (dt.Rows.Count > 0) btnQuanLyYeuCauNhap.Enabled = true;
            // Quản lý nhập mua vật tư
            btnNhapMua.Enabled = false;
            dt = cn.getTable("SELECT * FROM tbl_PhanQuyen WHERE MaQuyenHan='" + _MaQuyenHan + "' AND MaCapPhep='CP020'");
            if (dt.Rows.Count > 0) btnNhapMua.Enabled = true;
            // Quản lý nhập trả từ đơn vị sản xuất
            btnNhapTra.Enabled = false;
            dt = cn.getTable("SELECT * FROM tbl_PhanQuyen WHERE MaQuyenHan='" + _MaQuyenHan + "' AND MaCapPhep='CP021'");
            if (dt.Rows.Count > 0) btnNhapTra.Enabled = true;
            // Quản lý xuất cho đơn vị sản xuất
            btnXuatCongTrinh.Enabled = false;
            dt = cn.getTable("SELECT * FROM tbl_PhanQuyen WHERE MaQuyenHan='" + _MaQuyenHan + "' AND MaCapPhep='CP022'");
            if (dt.Rows.Count > 0) btnXuatCongTrinh.Enabled = true;
            // Quản lý xuất cho khách hàng
            btnXuatKhachHang.Enabled = false;
            dt = cn.getTable("SELECT * FROM tbl_PhanQuyen WHERE MaQuyenHan='" + _MaQuyenHan + "' AND MaCapPhep='CP023'");
            if (dt.Rows.Count > 0) btnXuatKhachHang.Enabled = true;
            // Quản lý đề nghị cấp vật tư
            btnDeNghiCap.Enabled = false;
            dt = cn.getTable("SELECT * FROM tbl_PhanQuyen WHERE MaQuyenHan='" + _MaQuyenHan + "' AND MaCapPhep='CP024'");
            if (dt.Rows.Count > 0) btnDeNghiCap.Enabled = true;
            // Quản lý chuyển kho
            btnChuyenKho.Enabled = false;
            dt = cn.getTable("SELECT * FROM tbl_PhanQuyen WHERE MaQuyenHan='" + _MaQuyenHan + "' AND MaCapPhep='CP025'");
            if (dt.Rows.Count > 0) btnChuyenKho.Enabled = true;
            // Quản lý hỏng mất
            btnHongMat.Enabled = false;
            dt = cn.getTable("SELECT * FROM tbl_PhanQuyen WHERE MaQuyenHan='" + _MaQuyenHan + "' AND MaCapPhep='CP026'");
            if (dt.Rows.Count > 0) btnHongMat.Enabled = true;

            /** Thống kê - Báo cáo **/
            // Báo cáo nhập kho
            btnBCNhapKho.Enabled = false;
            dt = cn.getTable("SELECT * FROM tbl_PhanQuyen WHERE MaQuyenHan='" + _MaQuyenHan + "' AND MaCapPhep='CP027'");
            if (dt.Rows.Count > 0) btnBCNhapKho.Enabled = true;
            // Báo cáo xuất kho
            btnBCXuatKho.Enabled = false;
            dt = cn.getTable("SELECT * FROM tbl_PhanQuyen WHERE MaQuyenHan='" + _MaQuyenHan + "' AND MaCapPhep='CP028'");
            if (dt.Rows.Count > 0) btnBCXuatKho.Enabled = true;
            // Báo cáo tồn kho
            btnBCTonKho.Enabled = false;
            dt = cn.getTable("SELECT * FROM tbl_PhanQuyen WHERE MaQuyenHan='" + _MaQuyenHan + "' AND MaCapPhep='CP029'");
            if (dt.Rows.Count > 0) btnBCTonKho.Enabled = true;
            // Báo cáo nhập - xuất - tồn
            btnBCNXT.Enabled = false;
            dt = cn.getTable("SELECT * FROM tbl_PhanQuyen WHERE MaQuyenHan='" + _MaQuyenHan + "' AND MaCapPhep='CP030'");
            if (dt.Rows.Count > 0) btnBCNXT.Enabled = true;
            // Báo cáo chuyển kho
            btnBCChuyenKho.Enabled = false;
            dt = cn.getTable("SELECT * FROM tbl_PhanQuyen WHERE MaQuyenHan='" + _MaQuyenHan + "' AND MaCapPhep='CP031'");
            if (dt.Rows.Count > 0) btnBCChuyenKho.Enabled = true;
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            if (cn.KetNoi())
            {
                frmDangNhap frm = new frmDangNhap();
                frm.ShowDialog();

                HienThiChucNangTheoQuyen();
            }
            else
            {
                if (!cn.KetNoi()) return;
                frmDangNhap frm = new frmDangNhap();
                frm.ShowDialog();

                HienThiChucNangTheoQuyen();
            }
        }

        private void btnCauHinh_Click(object sender, EventArgs e)
        {
            frmCauHinh frm = new frmCauHinh();
            frm.ShowDialog();
        }

        private void btnDoiMatKhau_Click(object sender, EventArgs e)
        {
            frmDoiMatKhau frm = new frmDoiMatKhau();
            frm.ShowDialog();
        }


        private void btnQuanLyNguoiDung_Click(object sender, EventArgs e)
        {
            frmNguoiDung frm = new frmNguoiDung();
            OpenTabForm(frm);

        }

        private void btnQuyenHan_Click(object sender, EventArgs e)
        {
            frmQuyenHan frm = new frmQuyenHan();
            OpenTabForm(frm);
        }

        private void btnSaoLuu_Click(object sender, EventArgs e)
        {
            frmSaoLuuDuLieu frm = new frmSaoLuuDuLieu();
            frm.ShowDialog();
        }

        private void btnPhucHoi_Click(object sender, EventArgs e)
        {
            frmPhucHoiDuLieu frm = new frmPhucHoiDuLieu();
            frm.ShowDialog();
        }

        private void btnDangXuat_Click(object sender, EventArgs e)
        {
            frmDangNhap frm = new frmDangNhap();
            frm.ShowDialog();
            tabMain.Tabs.Clear();
        }

        private void btnNhomVatTu_Click(object sender, EventArgs e)
        {
            frmNhomVatTu frm = new frmNhomVatTu();
            OpenTabForm(frm);
        }

        private void btnVatTu_Click(object sender, EventArgs e)
        {
            frmVatTu frm = new frmVatTu();
            OpenTabForm(frm);
        }

        private void btnKho_Click(object sender, EventArgs e)
        {
            frmKho frm = new frmKho();
            OpenTabForm(frm);
        }

        private void btnPhongBan_Click(object sender, EventArgs e)
        {
            frmPhongBan frm = new frmPhongBan();
            OpenTabForm(frm);
        }

        private void btnChucVu_Click(object sender, EventArgs e)
        {
            frmChucVu frm = new frmChucVu();
            OpenTabForm(frm);
        }

        private void btnDonViTinh_Click(object sender, EventArgs e)
        {
            frmDonViTinh frm = new frmDonViTinh();
            OpenTabForm(frm);
        }

        private void btnNhanVien_Click(object sender, EventArgs e)
        {
            frmNhanVien frm = new frmNhanVien();
            OpenTabForm(frm);
        }

        private void btnNhapMua_Click(object sender, EventArgs e)
        {
            frmQuanLyPhieuNhapMua frm = new frmQuanLyPhieuNhapMua();
            OpenTabForm(frm);
        }

        private void btnChuyenKho_Click(object sender, EventArgs e)
        {
            frmQuanLyChuyenKho frm = new frmQuanLyChuyenKho();
            OpenTabForm(frm);
        }

        private void btnXuatCongTrinh_Click(object sender, EventArgs e)
        {
            frmQuanLyPhieuXuatCongTrinhDuAn frm = new frmQuanLyPhieuXuatCongTrinhDuAn();
            OpenTabForm(frm);
        }

        private void btnNhapTra_Click(object sender, EventArgs e)
        {
            frmQuanLyNhapTraTuDonViSanXuat frm = new frmQuanLyNhapTraTuDonViSanXuat();
            OpenTabForm(frm);
        }

        private void btnBCNhapKho_Click(object sender, EventArgs e)
        {
            frmBaoCaoNhapKho frm = new frmBaoCaoNhapKho();
            OpenTabForm(frm);
        }

        private void btnBCXuatKho_Click(object sender, EventArgs e)
        {
            frmBaoCaoXuatKho frm = new frmBaoCaoXuatKho();
            OpenTabForm(frm);
        }

        private void btnBCTonKho_Click(object sender, EventArgs e)
        {
            frmBaoCaoTonKho frm = new frmBaoCaoTonKho();
            OpenTabForm(frm);
        }

        private void btnBCNXT_Click(object sender, EventArgs e)
        {
            frmBaoCaoNhapXuatTon frm = new frmBaoCaoNhapXuatTon();
            OpenTabForm(frm);
        }

        private void btnBCChuyenKho_Click(object sender, EventArgs e)
        {
            frmBaoCaoChuyenKho frm = new frmBaoCaoChuyenKho();
            OpenTabForm(frm);
        }

        private void btnCongTrinhDuAn_Click(object sender, EventArgs e)
        {
            frmCongTrinhDuAn frm = new frmCongTrinhDuAn();
            OpenTabForm(frm);
        }

        private void btnXuatKhachHang_Click(object sender, EventArgs e)
        {
            frmQuanLyXuatThanhLyKhachHang frm = new frmQuanLyXuatThanhLyKhachHang();
            OpenTabForm(frm);
        }

        private void btnDoiTac_Click(object sender, EventArgs e)
        {
            frmDoiTac frm = new frmDoiTac();
            OpenTabForm(frm);
        }

        private void btnDeNghiCap_Click(object sender, EventArgs e)
        {
            frmQuanLyPhieuDeNghiCapVatTu frm = new frmQuanLyPhieuDeNghiCapVatTu();
            OpenTabForm(frm);
        }

        private void btnPhieuXuatDonViSanXuat_Click(object sender, EventArgs e)
        {
            //frmPhieuXuatDonViSanXuat frm = new frmPhieuXuatDonViSanXuat();
            //OpenTabForm(frm);
        }

        private void btnNhaCungCap_Click(object sender, EventArgs e)
        {
            frmVatTuNhaCungCap frm = new frmVatTuNhaCungCap();
            OpenTabForm(frm);
        }

        private void btnQuanLyVatTuDuTru_Click(object sender, EventArgs e)
        {
            frmQuanLyDuTruVatTu frm = new frmQuanLyDuTruVatTu();
            OpenTabForm(frm);
        }

        private void btnQuanLyYeuCauNhap_Click(object sender, EventArgs e)
        {
            frmQuanLyYeuCauNhap frm = new frmQuanLyYeuCauNhap();
            OpenTabForm(frm);
        }

        private void buttonItem1_Click(object sender, EventArgs e)
        {
            frmDuTruVatTu frm = new frmDuTruVatTu();
            frm.ShowDialog();
        }

        private void btnHongMat_Click(object sender, EventArgs e)
        {
            frmQuanLyVatTuHongMat frm = new frmQuanLyVatTuHongMat();
            OpenTabForm(frm);
        }

        private void btnPhanQuyen_Click(object sender, EventArgs e)
        {
            frmPhanQuyen frm = new frmPhanQuyen();
            OpenTabForm(frm);
        }

        private void btnLichSuHeThong_Click(object sender, EventArgs e)
        {
            frmLichSuHeThong frm = new frmLichSuHeThong();
            OpenTabForm(frm);
        }

    }
}
